﻿using Factory.Customers;
using Factory.Customers.DTO;
using Factory.Cutomers;
using Factory.MasterData.MeasureUnits;
using Factory.MasterData.MeasureUnits.Dto;
using Factory.MesEntity.CustomerDatas;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Factory.MasterData.MaterialClassifications;
using Factory.MasterData.MaterialClassifications.Dto;
using Factory.MasterData.Materials;
using Factory.MasterData.Materials.Dto;
using Factory.MasterData.Vendors.Dto;
using Factory.MasterData.Vendors;
using Microsoft.EntityFrameworkCore.Query.Internal;
using Redis.Cluster.Demo;


namespace Factory.Web.Controllers
{

    /// <summary>
    /// 主数据控制器
    /// </summary>
    public class MainDataController : Controller
    {
        private readonly IMeasureUnitAppService _mainDataService;
        private readonly ICustomerDataAppService _customerDataService;
        /// <summary>
        /// 物料产品分类服务接口
        /// </summary>
        private readonly IMaterialClassificationAppService _materialClassificationAppService;
        /// <summary>
        /// 物料服务接口
        /// </summary>
        private readonly IMaterialAppService _materialAppService;
        /// <summary>
        /// 供应商服务接口
        /// </summary>
        private readonly IVendorAppService _vendorAppService;



        public MainDataController(IMeasureUnitAppService mainDataService, ICustomerDataAppService customerDataApp, IMaterialClassificationAppService materialClassificationAppService, IMaterialAppService materialAppService, IVendorAppService vendorAppService)
        {
            _mainDataService = mainDataService;
            _customerDataService = customerDataApp;
            _materialClassificationAppService = materialClassificationAppService;
            _materialAppService = materialAppService;
            _vendorAppService = vendorAppService;
        }



        #region 李金涛 页面  

        #region 视图

        /// <summary>
        /// 计量单位页面
        /// </summary>
        /// <returns></returns>
        public IActionResult MeasureUnitIndex()
        {
            return View();
        }

        /// <summary>
        /// 获取页面
        /// </summary>
        /// <returns></returns>
        public ActionResult UpdateMeasureUnitView(int id)
        {
            ViewBag.Id = id;
            return View();
        }

        /// <summary>
        /// 添加计量单位
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <returns>返回对应数据</returns>
        public IActionResult MeasureUnitAddView()
        {
            return View();
        }

        #endregion

        #region 接口

        /// <summary>
        /// 显示计量单位
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <returns>返回对应数据</returns>
        [HttpGet]
        public async Task<JsonResult> MeasureUnitShow(int page, int limit, string? unitCode, string? unitName)
        {
            var measureUnitlist = await _mainDataService.GetListAsync(page, limit, unitCode, unitName);
            return Json(new { code = 0, data = measureUnitlist });
        }


        /// <summary>
        /// 添加计量单位
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <returns>返回对应数据</returns>
        [HttpPost]
        public async Task<JsonResult> MeasureUnitAdd(MeasureUnit measureUnit)
        {
            var res = await _mainDataService.CreateAsync(measureUnit);
            return Json(new { code = res, msg = "添加成功" });
        }

        /// <summary>
        /// 删除计量单位
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <returns>返回对应数据</returns>
        [HttpPost]
        public async Task<JsonResult> MeasureUnitDelete(int id)
        {
            var res = await _mainDataService.DeleteAsync(id);
            return Json(new { code = res, msg = "删除成功" });
        }

        /// <summary>
        /// 编辑主计量单位
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> GetPrimaryUnit()
        {
            var res = await _mainDataService.GetPrimaryUnit();
            return Json(new { code = 0, data = res });
        }

        /// <summary>
        /// 生成一个随机的6位数编码
        /// </summary>
        /// <returns></returns>
        public string GenerateUnitCode()
        {
            // 示例：生成一个随机的6位数编码
            Random random = new Random();
            return random.Next(100000, 999999).ToString();
        }

        /// <summary>
        /// 获取计量单位详情页面
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> MeasureUnitOneView(int id)
        {
            var measureUnit = await _mainDataService.GetMeasureUnitOne(id);
            return Json(new { data = measureUnit });
        }

        /// <summary>
        ///  修改计量单位页面
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> UpdateMeasureUnit(MeasureUnitUpdateDto dto)
        {
            var res = await _mainDataService.UpdateAsync(dto);
            return Json(new { code = res });
        }
        #endregion

        #endregion

        #region 艾若彬 页面  
        public IActionResult CustomerDataIndex()
        {
            return View();
        }

        /// <summary>
        /// 获取客户数据
        /// </summary>
        /// <param name="customerName"></param>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetCustomerDataList(string? customerName, int page, int limit)
        {
            var customerlist = _customerDataService.GetCustomerDataList(customerName, page, limit);
            return Json(new { data = customerlist.Result, code = 0 });
        }

        public IActionResult CustomerDataAddIndex()
        {
            return View();
        }



        [HttpPost]
        public JsonResult AddCustomerData(CustomerData customerData)
        {
            var customerlist = _customerDataService.CreateAsync(customerData);
            return Json(new { code = customerlist, msg = "添加成功" });
        }

        /// <summary>
        /// 删除客户数据
        /// </summary>
        /// <returns>返回对应数据</returns>
        [HttpPost]
        public JsonResult CustomerDataDelete(int id)
        {
            var customerlist = _customerDataService.DeleteAsync(id);
            return Json(new { code = customerlist, msg = "删除成功" });
        }

        /// <summary>
        /// 获取页面
        /// </summary>
        /// <returns></returns>
        public ActionResult UpdateCustomerDatatView(int id)
        {
            ViewBag.Id = id;
            return View();
        }

        [HttpGet]
        /// <summary>
        /// 获取客户数据详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult GetCustomerById(int id)
        {
            var customerlist = _customerDataService.GetCustomerById(id);
            return Json(new { code = 0, data = customerlist });
        }
        [HttpPost]
        /// <summary>
        /// 更新客户数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public JsonResult UpdateCustomerData(CustomerDataUpdateDto input)
        {

            var customerlist = _customerDataService.UpdateAsync(input);
            return Json(new { code = true, data = customerlist });
        }
        #endregion

        #region 吴己心 页面  
        /// <summary>
        /// 物料产品管理页面
        /// </summary>
        /// <returns>显示页面</returns>
        public IActionResult MaterialManage()
        {
            return View();
        }
        /// <summary>
        /// 查询物料产品分类树形图
        /// </summary>
        /// <param name="product">是否是产品/物料</param>
        /// <param name="sysName">名称</param>
        /// <returns>返回结果</returns>
        [HttpGet]
        public async Task<JsonResult> GetMaterialClassificationTree(string product, string? sysName)
        {
            var list = await _materialClassificationAppService.GetMaterialClassificationTree(product, sysName);
            return Json(list);
        }
        /// <summary>
        /// 显示物料信息
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="materialCode">物料编码</param>
        /// <param name="materialName">物料名称</param>
        /// <param name="materialClassificationId">物料/产品</param>
        /// <returns>返回结果</returns>
        [HttpGet]
        public async Task<JsonResult> GetMaterialList(int pageIndex, int pageSize, string? materialCode, string? materialName, int? materialClassificationId)
        {
            var list = await _materialAppService.GetMaterialList(pageIndex, pageSize, materialCode, materialName, materialClassificationId);
            return Json(list);
        }
        /// <summary>
        /// 删除物料信息
        /// </summary>
        /// <param name="materialCodes">要删除的物料编码</param>
        /// <returns>返回结果</returns>
        [HttpPost]
        public async Task<int> DeletedMaterial(string materialCodes)
        {
            var reslut = await _materialAppService.DeletedMaterial(materialCodes);
            return reslut;
        }
        /// <summary>
        /// 显示物料产品分类信息
        /// </summary>
        /// <returns>返回结果</returns>
        [HttpGet]
        public  List<MaterialClassificationDto> GetMaterialClassification()
        {
            var result = _materialClassificationAppService.GetMaterialClassification();
            return result;
        }
        /// <summary>
        /// 查询计量单位,绑定下拉框
        /// </summary>
        /// <returns>返回结果</returns>
        [HttpGet]
        public async Task<List<MeasureUnitDto>> GetMeasureUnitDto()
        {
            var list = await _mainDataService.GetMeasureUnitDto();
            return list;
        }
        /// <summary>
        /// 添加物料信息
        /// </summary>
        /// <param name="materialDto">要添加的物料编码</param>
        /// <returns>返回结果</returns>
        [HttpPost]
        public async Task<int> AddMaterial(MaterialDto materialDto)
        {
            var result = await _materialAppService.AddMaterial(materialDto);
            return result;
        }
        /// <summary>
        /// 编辑物料信息
        /// </summary>
        /// <param name="materialDto">要编辑的物料编码</param>
        /// <returns>返回结果</returns>
        [HttpPost]
        public async Task<int> UpdateMaterial(MaterialDto materialDto)
        {
            var result = await _materialAppService.UpdateMaterial(materialDto);
            return result;
        }
        /// <summary>
        /// 供应商管理页面
        /// </summary>
        /// <returns>打开页面</returns>
        public IActionResult Supplierpage()
        {
            return View();
        }
        /// <summary>
        /// 添加供应商信息
        /// </summary>
        /// <param name="vendorDto">添加的供应商信息</param>
        /// <returns>返回结果</returns>
        [HttpPost]
        public async Task<int> AddVender(VendorDto vendorDto)
        {
            var result = await _vendorAppService.AddVender(vendorDto);
            return result;
        }
        /// <summary>
        /// 供应商信息分页显示
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="inquireName">供应商名称</param>
        /// <returns>返回结果</returns>
        [HttpGet]
        public async Task<JsonResult> GetVendorPage(int pageIndex, int pageSize, string? inquireName)
        {
            var list = await _vendorAppService.GetVendorPage(pageIndex, pageSize, inquireName);
            return Json(list);
        }
        /// <summary>
        /// 删除供应商信息
        /// </summary>
        /// <param name="id">供应商主键</param>
        /// <returns>返回结果</returns>
        [HttpPost]
        public async Task<int> DeleteVendor(string id)
        {
            var reslut = await _vendorAppService.DeleteVendor(id);
            return reslut;
        }
        /// <summary>
        /// 编辑供应商信息
        /// </summary>
        /// <param name="vendorDto">编辑的供应商信息</param>
        /// <returns>返回结果</returns>
        [HttpPost]
        public async Task<int> UpdateVender(VendorDto vendorDto)
        {
            var reslut = await _vendorAppService.UpdateVender(vendorDto);
            return reslut;
        }
        /// <summary>
        /// 编辑重复性验证
        /// </summary>
        /// <param name="code">供应商编码</param>
        /// <param name="id">供应商主键</param>
        /// <returns>返回结果</returns>
        [HttpGet]
        public async Task<int> UpdateRepeatability(string code, int id)
        {
            var reslut = await _vendorAppService.UpdateRepeatability(code, id);
            return reslut;
        }
        #endregion
    }
}
